package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.RoleService;
import com.itheima.UserService;
import com.itheima.contant.MessageConstant;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.*;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    /*根据用户名查询用户信息*/
    @Override
    public User findByUserName(String username) {
        /*返回的 user对象中要包含该用户所有的角色和这些角色所拥有的所有权限*/

        User user = userDao.findByUserName(username);
        if (user == null){
            return null;
        }
        Integer userId = user.getId();
        /*使用userId获取角色列表*/
        Set<Role> roles = roleDao.queryRolesByUserId(userId);

        for (Role role : roles) {
            Integer roleId = role.getId();
            /*使用roleId获取权限列表*/
            Set<Permission> permissions = permissionDao.queryPermissionsByRoleId(roleId);
            role.setPermissions(permissions);
        }

        user.setRoles(roles);

        return user;
    }


    @Override
    public Set<Role> findRoleById(Integer id) {

        return userDao.findRoleById(id);
    }


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<User> userList =userDao.findPageByQeryString(queryPageBean.getQueryString());
        for (User user : userList) {
            Set<Role> roles = roleDao.getRoleByUSerId(user.getId());
            user.setRoles(roles);
        }
        return new PageResult(page.getTotal(),userList);
    }


    @Override
    public List<Integer> findRoleIdByUserId(Integer id) {


        return userDao.findRoleIdByUserId(id);
    }

    @Override
    public User findUserById(Integer id) {

        return userDao.findUserById(id);
    }

    @Override
    public void editRole(User user) {
//        首先删除关联表中的关系
        userDao.deleteRelationByUserId(user.getId());
//        然后再添加进去
        List<Integer> roleIds = user.getRoleIds();
        setUSerAndRole(user.getId(), roleIds);
    }

    @Override
    public void deleteById(Integer id) {
//        查询是否关联其它表，关联了就直接删除
        Set<Role> role = userDao.findRoleById(id);
        if (role != null) {
            userDao.deleteRelationByUserId(id);
        }
        userDao.deleteById(id);
    }

    @Override
    public List<User> findUserByRoleId(Integer id) {

        return userDao.findUserByRoleId(id);
    }

    public void setUSerAndRole(Integer userId,List<Integer> roleIds ){
        List<Map> list = new ArrayList<>();
        if(roleIds != null && roleIds.size() > 0){
            for (Integer roleId : roleIds) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("userId", userId);
                map.put("roleId",roleId);
                list.add(map);
            }
            userDao.setUSerAndRoleBylist(list);
        }

    }

}
